package com.dev.engine.image;


import ohos.agp.components.Image;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.media.image.PixelMap;

/**
 * detail: Image Engine
 * @author Ttt
 */
public final class DevImageEngine {

    private DevImageEngine() {
    }

    private static IImageEngine sEngine;

    /**
     * 获取 ImageEngine
     * @return {@link IImageEngine}
     */
    public static IImageEngine getEngine() {
        return sEngine;
    }

    /**
     * 设置 ImageEngine
     * @param engine {@link IImageEngine}
     */
    public static void setEngine(final IImageEngine engine) {
        DevImageEngine.sEngine = engine;
    }

    public static void setImageByUi(Image imageByUi, String url, Context context) {
        /**
         * 读取网络数据的耗时操作在TaskDispatcher分发的单独线程中处理，
         * 否则会报 "NetworkOnMainThreadException"异常
         */

        TaskDispatcher refreshUITask = context.createParallelTaskDispatcher(url, TaskPriority.DEFAULT);
        refreshUITask.syncDispatch(() -> {
            PixelMap pixelMap = ImageReadUtil.createPixelMap(url, context);
            //Image组件填充位图数据，ui界面更新

            imageByUi.setPixelMap(pixelMap);
            pixelMap.release();

        });
    }
}